﻿using Hotel.Model.Shop;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hotel.DAL
{
    public class ShopDAL
    {
        public ShopDAL(HotelConText hotelConText)
        {
            HotelConText = hotelConText;
        }

        public HotelConText HotelConText { get; }






        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="shopModel"></param>
        /// <returns></returns>
        public int ShopAdd(ShopModel shopModel)
        {
            HotelConText.shops.Add(shopModel);
            return HotelConText.SaveChanges();

        }




        /// <summary>
        ///显示
        /// </summary>
        /// <param name="count"></param>
        /// <param name="page"></param>
        /// <param name="ShopName">门店名称</param>
        /// <param name="ShopType">服务范围</param>
        /// <param name="index"></param>
        /// <param name="size"></param>
        /// <returns></returns>

        public List<ShopModel> ShopList(out int count, out int page, string? ShopName="", string? ShopType="-1", int index = 1, int size = 2)
        {

            var list = HotelConText.shops.Where(x => x.IsDeleted == false).AsQueryable();
            if (!string.IsNullOrWhiteSpace(ShopName))
            {

                list = list.Where(x => x.ShopName.Contains(ShopName));
            }

            if (ShopType != "-1")
            {
                list = list.Where(x => x.ShopType == ShopType);

            }

            count = list.Count();
            page = (int)Math.Ceiling(count * 1.0 / size);
            return list.OrderBy(x => x.Id).Skip((index - 1) * size).Take(size).ToList();

        }





        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int ShopDel(int id)
        {

            var ShopDels = HotelConText.shops.Find(id);
            ShopDels.IsDeleted = true;
            return HotelConText.SaveChanges();
        }






        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ShopModel ShopAll(int id)
        {
            return HotelConText.shops.Find(id);

        }



        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="shopModel"></param>
        /// <returns></returns>
        public int ShopUpd(ShopModel shopModel)
        {
            var ShopUpds = HotelConText.shops.Find(shopModel.Id);
            if (ShopUpds != null)
            {
                ShopUpds.ShopName = shopModel.ShopName;
                ShopUpds.CreateTime = shopModel.CreateTime;
                ShopUpds.ShopType = shopModel.ShopType;
                ShopUpds.Name = shopModel.Name;
                ShopUpds.Phone = shopModel.Phone;
                ShopUpds.JieShao = shopModel.JieShao;
                ShopUpds.Adder = shopModel.Adder;
                ShopUpds.State = shopModel.State;
                ShopUpds.Photo = shopModel.Photo;
            }
            return HotelConText.SaveChanges();
        }




        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="ids"></param>
        /// <param name="State"></param>
        /// <returns></returns>
        public int UpdState(string ids, int State)
        {
            var idsArr = ids.Split(",");

            foreach (var id in idsArr)
            {
                var infor = HotelConText.shops.Find(Convert.ToInt32(id));
                infor.State = State;

            }
            return HotelConText.SaveChanges();

        }

    }
}
